#include "gamecontrolwindow.h"
#include <QVBoxLayout>

GameControlWindow::GameControlWindow(QWidget *parent)
    : QWidget(parent), elapsedSeconds(0), isPaused(false)
{

        // 设置窗口属性
        setWindowTitle("菜单");
        setFixedSize(200, 150);

    //新加
    setStyleSheet("QWidget { background: #f0f0f0; padding: 10px; }"
                  "QPushButton { min-width: 100px; padding: 5px; }");



    // 初始化控件
    timeLabel = new QLabel("00:00", this);
    timeLabel->setAlignment(Qt::AlignCenter);
    timeLabel->setStyleSheet("font-size: 20px;");

    pauseButton = new QPushButton("暂停", this);
    returnButton = new QPushButton("返回主界面", this);

    // 设置布局
    QVBoxLayout *layout = new QVBoxLayout(this);
    layout->addWidget(timeLabel);
    layout->addWidget(pauseButton);
    layout->addWidget(returnButton);

    // 初始化定时器
    timer = new QTimer(this);
    timer->setInterval(1000); // 1秒间隔

    // 连接信号槽
    connect(timer, &QTimer::timeout, this, &GameControlWindow::updateTimeDisplay);
    connect(pauseButton, &QPushButton::clicked, this, &GameControlWindow::togglePause);
    connect(returnButton, &QPushButton::clicked, this, &GameControlWindow::returnToMain);


    connect(returnButton, &QPushButton::clicked, this, [this](){
        emit returnToMain();
    });

}

void GameControlWindow::startTimer()
{
    elapsedSeconds = 0;
    isPaused = false;
    timer->start();
    pauseButton->setText("暂停");
    updateTimeDisplay();
}

void GameControlWindow::resetTimer()
{
    elapsedSeconds = 0;
    updateTimeDisplay();
}

void GameControlWindow::updateTimeDisplay()
{
    if(!isPaused) {
        elapsedSeconds++;
        int minutes = elapsedSeconds / 60;
        int seconds = elapsedSeconds % 60;
        timeLabel->setText(QString("%1:%2")
                               .arg(minutes, 2, 10, QLatin1Char('0'))
                               .arg(seconds, 2, 10, QLatin1Char('0')));
    }
}

void GameControlWindow::togglePause()
{
    isPaused = !isPaused;
    if(isPaused) {
        timer->stop();
        pauseButton->setText("继续");
    } else {
        timer->start();
        pauseButton->setText("暂停");
    }
    emit pauseStateChanged(isPaused);
}


void GameControlWindow::stopTimer() {
    timer->stop();
    isPaused = true; // 确保状态同步
    pauseButton->setText("结束");
}



